package com.gxa.clientarticle.service.impl;

/*
 *
 *@author leijun
 *
 *@creating 2020/11/13 9:55
 *
 */

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.clientarticle.mapper.ArtcleMapper;
import com.gxa.clientarticle.pojo.Article;
import com.gxa.clientarticle.service.ArtcleService;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ArtcleServiceImpl
 * @Description TODO
 * @Author 俊
 * @Date 2020/11/13 9:55
 * @Version 1.0
 */
@Service("artcleService")
public class ArtcleServiceImpl implements ArtcleService {

    @Resource
    ArtcleMapper  artcleMapper;


    @Override
    public Integer add(Article article) {
        return artcleMapper.insertSelective(article);
    }

    @Override
    public Map list(Integer page,Integer limit) {
        PageHelper.startPage(page,limit);
        HashMap<String, Object> map = new HashMap<>();
        List<Article> articles = artcleMapper.selectAll();
        PageInfo<Article> info = new PageInfo<>(articles);
        map.put("list",info.getList());
        map.put("count",info.getTotal());
        return map;
    }

    @Override
    public Map typeList(Integer type, Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        HashMap<String, Object> map = new HashMap<>();
        Example example = new Example(Article.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("type",type);
        List<Article> articles = artcleMapper.selectByExample(example);
        PageInfo<Article> info = new PageInfo<>(articles);
        map.put("list",info.getList());
        map.put("count",info.getTotal());
        return map;
    }

    @Override
    public Map listTitle(String name, Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        HashMap<String, Object> map = new HashMap<>();
        Example example = new Example(Article.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.orLike("name","%"+name+"%");
        List<Article> articles = artcleMapper.selectByExample(example);
        PageInfo<Article> info = new PageInfo<>(articles);
        map.put("list",info.getList());
        map.put("count",info.getTotal());
        return map;
    }

    @Override
    public Integer update(Article article) {
        return artcleMapper.updateByPrimaryKeySelective(article);
    }

    @Override
    public Integer updateState(Integer id, Boolean state) {
        Example example = new Example(Article.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",id);
        Article article = new Article();
        article.setState(state);
        return artcleMapper.updateByExampleSelective(article,example);
    }

    @Override
    public Integer delete(Integer id) {
        return artcleMapper.deleteByPrimaryKey(id);
    }
}
